import { Component, OnInit } from '@angular/core';

import { Http, Jsonp } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/Rx';

@Component({
  selector: 'app-today',
  templateUrl: './today.component.html',
  styleUrls: ['./today.component.less']
})
export class TodayComponent implements OnInit {
  dataContent = [];
  // 参数配置
  todayUrl = 'http://route.showapi.com/856-1';
  showapi_appid = '59577';
  showapi_sign = '761c8417b8da43e5b184ac18ce329698';

  // 获取当天日期
  dateNow = new Date();
  year = this.dateNow.getFullYear();
  month = this.dateNow.getMonth() + 1;
  day = this.dateNow.getDate();

  dateToday: any;   /*需要传递的时间参数*/
  // 时间处理函数
  formatNumber = n => {
    n = n.toString();
    return n[1] ? n : '0' + n;
  }

  getData() {
    // 拼接成需要的格式
    this.dateToday = this.year + '' + this.formatNumber(this.month) + '' + this.formatNumber(this.day);

    // 保存一份指针
    const that = this;
    this.http.get(this.todayUrl + '?showapi_appid=' + this.showapi_appid + '&showapi_sign=' + this.showapi_sign + '&date=' + this.dateToday)
      .map(res => res.json())
      .subscribe(function (data) {
        that.dataContent = data.showapi_res_body;
        // console.log(data);
      }, function (err) {

      });
  }
constructor(private http: Http, private jsonp: Jsonp) {
 }

  ngOnInit() {
    this.getData();
  }

}
